package com.jtoon.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jtoon.bean.Comics;
import com.jtoon.bean.Episode;
import com.jtoon.bean.Img;
import com.jtoon.util.PMF;

public class ImgServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		int eOrder = Integer.valueOf(request.getParameter("eOrder"));
		int imgNum = 1;
		long cid = Long.valueOf(request.getParameter("cid"));
		long eid = Long.valueOf(request.getParameter("eid"));
		
		Query comicsListQry = pm.newQuery(Comics.class);
		Query episodeListQry = pm.newQuery(Episode.class);
		Query epiQry = pm.newQuery(Episode.class);
		Query query = pm.newQuery(Img.class);
		
		HttpSession session = request.getSession();
		if(session.getAttribute("id") != null){
			request.setAttribute("isLogin", true);
		}
		
		try{ 
			Comics comics = pm.getObjectById(Comics.class, cid);
			String comicsFolder = comics.getTitleImg().replace("img/", "").replace(".jpg", "");
			request.setAttribute("title", comics.getTitle());
			request.setAttribute("comicsFolder", comicsFolder);
			
			if(request.getParameter("imgNum") != null){
				imgNum = Integer.valueOf(request.getParameter("imgNum"));
			}
			  
			request.setAttribute("imgNum", imgNum);
			
			episodeListQry.setFilter("cid == cidParam");
			episodeListQry.declareParameters("long cidParam");
			episodeListQry.setOrdering("order desc");   
			List<Episode> episodeList = (List<Episode>)episodeListQry.execute(cid);
			
			//처음 에피소드 
			Episode lastEpisode = episodeList.get(0);
			request.setAttribute("lastEpisode", lastEpisode);
			 
			//마지막 에피소드
			Episode firstEpisode = episodeList.get(episodeList.size()-1);
			request.setAttribute("firstEpisode", firstEpisode);
			 
			Episode episode = null;
			Episode nextEpisode = null;
			Episode prevEpisode = null;
			
			for(int i=0; i<episodeList.size(); i++){
				Episode epi = episodeList.get(i);
				
				if(epi.getOrder() == eOrder){
					episode = epi;
					if(i < episodeList.size()-1){
						prevEpisode = episodeList.get(i + 1);
					}
					if(0 < i){
						nextEpisode = episodeList.get(i - 1);
					} 
					i = episodeList.size();   
				} 
				  
			}
			 
			query.setFilter("eid == eidParam");
			query.declareParameters("long eidParam");	
			query.setOrdering("order asc");   
			List<Img> imgList = (List<Img>)query.execute(episode.getEid().getId()); 
			if(imgList != null && imgList.size() > 0){
				if(imgNum < imgList.size()){
					request.setAttribute("img", imgList.get(imgNum-1));
				}else if(imgList.size() <= imgNum){
					request.setAttribute("img", imgList.get(imgList.size() - 1));
				} 
			}   
			
			request.setAttribute("imgList", imgList);
			request.setAttribute("episode", episode);
			request.setAttribute("nextEpisode", nextEpisode);
			request.setAttribute("prevEpisode", prevEpisode);
			request.setAttribute("cid", cid);
			request.setAttribute("eid", eid); 
			RequestDispatcher rd = request.getRequestDispatcher("/page/view.jsp"); 
			rd.forward(request, response);  
		}finally{  
			query.closeAll();  
			epiQry.closeAll();
			episodeListQry.closeAll();
			pm.close();   
		} 
		
	}
	
}
